unit unTimer;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls, MMSystem;

type

  { TfrmTimer }

  TfrmTimer = class(TForm)
    btnStart: TButton;
    btnStop: TButton;
    btn5: TButton;
    btn10: TButton;
    edtMinutes: TEdit;
    edtseconds: TEdit;
    timTask: TTimer;
    udMinutes: TUpDown;
    udSeconds: TUpDown;
    procedure btn10Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure timTaskTimer(Sender: TObject);
    procedure udMinutesClick(Sender: TObject; Button: TUDBtnType);
    procedure udSecondsClick(Sender: TObject; Button: TUDBtnType);
  private
    { private declarations }
    Minutes, seconds : Integer;
  public
    { public declarations }
  end; 

implementation

{ TfrmTimer }

procedure TfrmTimer.btn5Click(Sender: TObject);
begin
  edtMinutes.Text := '5';
  edtSeconds.Text := '00';
  Minutes := 5;
  seconds := 0;
  btnStartClick(Sender);
end;

procedure TfrmTimer.btnStartClick(Sender: TObject);
begin
  if (edtMinutes.text <> 'Minutes') then
  begin
     Minutes := StrToInt(edtMinutes.Text);
     Seconds := StrToInt(edtSeconds.Text);
     timTask.Enabled:=True;
  end
  else
      showmessage('Minutes to be added first');
end;

procedure TfrmTimer.btnStopClick(Sender: TObject);
begin
  timTask.Enabled:=False;
end;

procedure TfrmTimer.timTaskTimer(Sender: TObject);
begin
  if (seconds = 0) then
  begin
       if (minutes = 0) then
       begin
          PlaySound('alarm_3.wav',0,0);
          timTask.Enabled := False;
       end
       else
       begin
           dec(Minutes);
           seconds := 59;
       end;
  end
  else
      dec(seconds);
  edtMinutes.Text := IntToStr(Minutes);
  edtSeconds.Text := IntToStr(Seconds);
end;

procedure TfrmTimer.udMinutesClick(Sender: TObject; Button: TUDBtnType);
var
   iMinutes : Integer;
begin
     if not(timTask.Enabled)  then
     begin
         iMinutes := StrToInt(edtMinutes.Text);
         if Button = btNext then
            inc(iMinutes)
         else
             dec(iMinutes);
         edtMinutes.Text:= IntToStr(iMinutes);
     end
     else
         ShowMessage('Please stop the timer before increasing the time!');
end;

procedure TfrmTimer.udSecondsClick(Sender: TObject; Button: TUDBtnType);
var
   iSeconds : Integer;
begin
     if not(timTask.Enabled)  then
     begin
         iSeconds := StrToInt(edtSeconds.Text);
         if Button = btNext then
            inc(iSeconds)
         else
             dec(iSeconds);
         edtSeconds.Text:= IntToStr(iSeconds);
     end
     else
         ShowMessage('Please stop the timer before increasing the time!');
end;

procedure TfrmTimer.btn10Click(Sender: TObject);
begin
  edtMinutes.Text := '10';
  edtSeconds.Text := '00';
  Minutes := 10;
  seconds := 0;
  btnStartClick(Sender);
end;

initialization
  {$I untimer.lrs}

end.

